package com.xiaojie.summer.factory;

import com.xiaojie.summer.exception.BeansException;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @author Nick
 * @date 2021/5/22
 * @description
 */
public class WebApplicationContext implements ApplicationContext {

    Map<String, Object> nameBeanMap = new HashMap<>();
    Map<Class, Object> classBeanMap = new HashMap<>();

    Map<String, Object> tempNameBeanMap = new HashMap<>();
    Map<Class, Object> tempClassBeanMap = new HashMap<>();

    Map<String, String> requestMap = new HashMap<>();

    Properties properties = new Properties();

    String appName = "";

    @Override
    public Object getBean(String name) throws BeansException {
        return nameBeanMap.get(name);
    }

    @Override
    public <T> T getBean(String name, Class<T> requiredType) throws BeansException {

        Object bean = this.getBean(name);
        if (requiredType != null && !requiredType.isInstance(bean)) {
            throw new BeansException(name);
        } else {
            return (T) bean;
        }

    }

    @Override
    public Object getBean(Class type) throws BeansException {
        Object result = classBeanMap.get(type);
        if (result == null) {

            for (Class clazz : classBeanMap.keySet()) {
                for (Class c : clazz.getInterfaces()) {
                    if (type == c) {
                        return classBeanMap.get(clazz);
                    }
                }
            }

        }
        return result;
    }


    @Override
    public String getId() {
        return appName;
    }

    @Override
    public String getApplicationName() {
        return appName;
    }

    @Override
    public String getDisplayName() {
        return appName;
    }

    public Map<String, Object> getNameBeanMap() {
        return nameBeanMap;
    }

    public void setNameBeanMap(Map<String, Object> nameBeanMap) {
        this.nameBeanMap = nameBeanMap;
    }

    public Map<Class, Object> getClassBeanMap() {
        return classBeanMap;
    }

    public void setClassBeanMap(Map<Class, Object> classBeanMap) {
        this.classBeanMap = classBeanMap;
    }

    public Map<String, Object> getTempNameBeanMap() {
        return tempNameBeanMap;
    }

    public void setTempNameBeanMap(Map<String, Object> tempNameBeanMap) {
        this.tempNameBeanMap = tempNameBeanMap;
    }

    public Map<Class, Object> getTempClassBeanMap() {
        return tempClassBeanMap;
    }

    public void setTempClassBeanMap(Map<Class, Object> tempClassBeanMap) {
        this.tempClassBeanMap = tempClassBeanMap;
    }

    public String getAppName() {
        return appName;
    }

    public void setAppName(String appName) {
        this.appName = appName;
    }

    public Properties getProperties() {
        return properties;
    }

    public void setProperties(Properties properties) {
        this.properties = properties;
    }

    public Map<String, String> getRequestMap() {
        return requestMap;
    }

    public void setRequestMap(Map<String, String> requestMap) {
        this.requestMap = requestMap;
    }
}
